package com.open.common.util;

import java.security.SecureRandom;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 封装各种生成唯一性ID算法的工具类.
 */
public class IdGen {

	private static SecureRandom random = new SecureRandom();

	public static final DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
	public static final DateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	public static final DateFormat df4 = new SimpleDateFormat("yyMMddHHmmss");

	/**
	 * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
	 */
	public static String uuid() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 * 使用SecureRandom随机生成Long.
	 */
	public static long randomLong() {
		return Math.abs(random.nextLong());
	}

	// 生成25位ID
	public static String gen25Id() {
		Random rand = new Random();
		String result = df3.format(new Date()) + (rand.nextInt(89999999) + 10000000);
		return result;
	}

	// 生成16位ID
	public static String gen16Id() {
		Random rand = new Random();
		String result = df4.format(new Date()) + (rand.nextInt(8999) + 1000);
		return result;
	}

	public static void main(String[] args) {
		System.out.println(IdGen.uuid());
		System.out.println(IdGen.uuid().length());
	}

}
